/*
 * Copyright (c) 2012-2013, Charles Santos da Silva <silva.charlessantos@gmail.com> -
 * scharlessantos.org
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE
 */
package org.scharlessantos.hermes.logger;

import org.scharlessantos.hermes.HConfiguration;
import org.scharlessantos.hermes.writer.HLevel;
import org.scharlessantos.hermes.writer.HWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Hermes Log File Writer implementation for general puporse<br/>
 * 
 * Save the log in a file definied in {@link HConfiguration#setLogFile(File)}
 * 
 * Hermes is free open-source Java Logger.<br>
 * It receive this name because Hermes was the messenger of Olympians gods, so,
 * anybody better than Hermes to send system messages to programmer.<br>
 * <br>
 * * <br>
 * Hermes is licensed by new BSD License. A notice of license can be saw below<br>
 * <br>
 * <i>
 * Copyright (c) 2012-2013, Charles Santos da Silva <silva.charlessantos@gmail.com> -
 * scharlessantos.org
 * All rights reserved.<br>
 * <br>
 * Redistribution and use in source and binary forms, with or without<br>
 * modification, are permitted provided that the following conditions are met:<br>
 * <br>
 * 1. Redistributions of source code must retain the above copyright notice,<br>
 * this list of conditions and the following disclaimer.<br>
 * 2. Redistributions in binary form must reproduce the above copyright<br>
 * notice, this list of conditions and the following disclaimer in the<br>
 * documentation and/or other materials provided with the distribution.<br>
 * <br>
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"<br>
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE<br>
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br>
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF<br>
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS<br>
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN<br>
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)<br>
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE<br>
 * POSSIBILITY OF SUCH DAMAGE<br>
 * </i>
 * 
 * @author Charles Santos da Silva
 */
public class HFileLogger implements HWriter {
	
	private static HFileLogger log;
	
	/**
	 * Gets a instance of Hermes Generic File Log Writer
	 */
	public static HWriter getInstance() {
		if (log == null)
			log = new HFileLogger();
		
		return log;
	}
	
	@Override
	public void logMessage(HLevel level, StackTraceElement source, String message) {
		saveLog(String.format(HConfiguration.getInstance().getBaseFormat() + ": %s", level.name(), source.getClassName(), source.getMethodName(), source.getFileName(), source.getLineNumber(), Thread.currentThread().getId(), message), null);
	}
	
	@Override
	public void logException(HLevel level, StackTraceElement source, Throwable exception) {
		saveLog(String.format(HConfiguration.getInstance().getBaseFormat() + ": Exception", level.name(), source.getClassName(), source.getMethodName(), source.getFileName(), source.getLineNumber(), Thread.currentThread().getId()), exception);
	}
	
	private static DateFormat  timeFmt = new SimpleDateFormat("HH:mm:ss");
	private static PrintWriter wr;
	
	/**
	 * Writes log in file
	 * 
	 * @param msg Message to write
	 * @param exception to write
	 */
	private static synchronized void saveLog(String msg, Throwable exception) {
		try {
			Date now = new Date();
			
			if (wr != null)
				wr.close();
			
			File file = HConfiguration.getInstance().getLogFile();
			
			if (file != null && !file.isDirectory()) {
				wr = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
				wr.print(timeFmt.format(now));
				wr.print(" ");
				wr.println(msg);
				if (exception != null)
					exception.printStackTrace(wr);
				wr.flush();
				
			}
		} catch (IOException io) {
			io.printStackTrace();
		}
	}
	
}
